package com.wechat.page.chat.misc;

import com.swak.fx.support.ui.UI;
import com.swak.fx.support.ui.UICell;
import com.swak.fx.support.ui.UIIcon;
import com.swak.fx.support.ui.UILabel;
import com.swak.fx.support.ui.UILifecycle;
import com.swak.fx.support.ui.UITextField;
import com.swak.utils.StringUtils;
import com.wechat.dto.ChatRoomDTO;
import com.wechat.dto.ContactDTO;
import com.wechat.page.chat.popup.AnnouncementPage;

import javafx.beans.binding.Bindings;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.scene.control.Label;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Priority;

/**
 * 群聊的表单
 * 
 * @author DELL
 */
public class ProfileForm extends GridPane implements UILifecycle {

	private final String[] STYLE_CLASS = { "profile_form" };

	private final Label nameLabel;
	private final Label nameText;
	private final UITextField nameField;
	private final UIIcon nameIcon;
	private final UICell nameCell;

	private final Label announcementLabel;
	private final Label announcementText;
	private final UIIcon announcementIcon;
	private final UICell announcementCell;

	private final Label aliasLabel;
	private final Label aliasText;
	private final UITextField aliasField;
	private final UIIcon aliasIcon;
	private final UICell aliasCell;

	private final Label displayNameLabel;
	private final Label displayNameText;
	private final UITextField displayNameField;
	private final UIIcon displayNameIcon;
	private final UICell displayNameCell;

	public ProfileForm() {
		this.getStyleClass().addAll(STYLE_CLASS);

		// 群聊名称
		this.nameLabel = new UILabel("群聊名称");
		this.nameLabel.getStyleClass().add("profile_form--title");
		this.nameCell = new UICell();
		this.nameText = new UILabel("设置群聊名称");
		this.nameField = new UITextField().MaxWord(30);
		this.nameField.setPromptText("设置群聊名称");
		this.nameIcon = new UIIcon().Icon(UI.Icons.EDIT).fixedWidth();

		// 群聊名称
		this.announcementLabel = new UILabel("群公告");
		this.announcementLabel.getStyleClass().add("profile_form--title");
		this.announcementCell = new UICell();
		this.announcementText = new UILabel("设置群公告");
		this.announcementIcon = new UIIcon().Icon(UI.Icons.RIGHT).fixedWidth();

		// 备注
		this.aliasLabel = new UILabel("群聊备注");
		this.aliasLabel.getStyleClass().add("profile_form--title");
		this.aliasCell = new UICell();
		this.aliasText = new UILabel("设置群聊备注");
		this.aliasField = new UITextField().MaxWord(30);
		this.aliasField.setPromptText("设置群聊备注");
		this.aliasIcon = new UIIcon().Icon(UI.Icons.EDIT).fixedWidth();

		// 我在本群的昵称
		this.displayNameLabel = new UILabel("我在本群的昵称");
		this.displayNameLabel.getStyleClass().add("profile_form--title");
		this.displayNameCell = new UICell();
		this.displayNameText = new UILabel("设置我在本群的昵称");
		this.displayNameField = new UITextField().MaxWord(30);
		this.displayNameField.setPromptText("设置我在本群的昵称");
		this.displayNameIcon = new UIIcon().Icon(UI.Icons.EDIT).fixedWidth();

		this.init();
	}

	@Override
	public void initializeParts() {
		this.getChildren().add(this.nameLabel);
		this.getChildren().add(this.nameCell);
		GridPane.setConstraints(this.nameLabel, 0, 0);
		GridPane.setHgrow(this.nameLabel, Priority.ALWAYS);
		GridPane.setVgrow(this.nameLabel, Priority.ALWAYS);
		GridPane.setConstraints(this.nameCell, 0, 1);
		GridPane.setHgrow(this.nameCell, Priority.ALWAYS);
		GridPane.setVgrow(this.nameCell, Priority.ALWAYS);

		this.getChildren().add(this.announcementLabel);
		this.getChildren().add(this.announcementCell);
		GridPane.setConstraints(this.announcementLabel, 0, 2);
		GridPane.setHgrow(this.announcementLabel, Priority.ALWAYS);
		GridPane.setVgrow(this.announcementLabel, Priority.ALWAYS);
		GridPane.setConstraints(this.announcementCell, 0, 3);
		GridPane.setHgrow(this.announcementCell, Priority.ALWAYS);
		GridPane.setVgrow(this.announcementCell, Priority.ALWAYS);

		this.getChildren().add(this.aliasLabel);
		this.getChildren().add(this.aliasCell);
		GridPane.setConstraints(this.aliasLabel, 0, 4);
		GridPane.setHgrow(this.aliasLabel, Priority.ALWAYS);
		GridPane.setVgrow(this.aliasLabel, Priority.ALWAYS);
		GridPane.setConstraints(this.aliasCell, 0, 5);
		GridPane.setHgrow(this.aliasCell, Priority.ALWAYS);
		GridPane.setVgrow(this.aliasCell, Priority.ALWAYS);

		this.getChildren().add(this.displayNameLabel);
		this.getChildren().add(this.displayNameCell);
		GridPane.setConstraints(this.displayNameLabel, 0, 6);
		GridPane.setHgrow(this.displayNameLabel, Priority.ALWAYS);
		GridPane.setVgrow(this.displayNameLabel, Priority.ALWAYS);
		GridPane.setConstraints(this.displayNameCell, 0, 7);
		GridPane.setHgrow(this.displayNameCell, Priority.ALWAYS);
		GridPane.setVgrow(this.displayNameCell, Priority.ALWAYS);
	}

	@Override
	public void setupEventHandlers() {
		this.addEventHandler(MouseEvent.MOUSE_CLICKED, (e) -> {
			this.nameEditingProperty.set(false);
			this.aliasEditingProperty.set(false);
			this.displayNameEditingProperty.set(false);
		});
		this.nameCell.addEventHandler(MouseEvent.MOUSE_CLICKED, (e) -> {
			e.consume();
			this.nameEditingProperty.set(true);
		});
		this.announcementCell.addEventHandler(MouseEvent.MOUSE_CLICKED, (e) -> {
			AnnouncementPage.New().show();
		});
		this.aliasCell.addEventHandler(MouseEvent.MOUSE_CLICKED, (e) -> {
			e.consume();
			this.aliasEditingProperty.set(true);
		});
		this.displayNameCell.addEventHandler(MouseEvent.MOUSE_CLICKED, (e) -> {
			e.consume();
			this.displayNameEditingProperty.set(true);
		});
	}

	@Override
	public void setupBindings() {
		this.nameText.textProperty().bind(Bindings.createStringBinding(() -> {
			return StringUtils.defaultIfBlank(this.nameEditProperty.get(), "设置群聊名称");
		}, this.nameEditProperty));
		this.aliasText.textProperty().bind(Bindings.createStringBinding(() -> {
			return StringUtils.defaultIfBlank(this.aliasEditProperty.get(), "设置群聊备注");
		}, this.aliasEditProperty));
		this.displayNameText.textProperty().bind(Bindings.createStringBinding(() -> {
			return StringUtils.defaultIfBlank(this.displayNameEditProperty.get(), "设置我在本群的昵称");
		}, this.displayNameEditProperty));
	}

	@Override
	public void setupValueChangedListeners() {
		this.nameEditingProperty.addListener((ob, o, n) -> {
			if (n) {
				this.nameCell.valueNodeProperty().set(this.nameField);
				this.nameField.setText(this.nameEditProperty.get());
				this.nameField.requestFocus();
			} else {
				this.nameCell.valueNodeProperty().set(this.nameText);
				this.nameEditProperty.set(StringUtils.trimToEmpty(this.nameField.getText()));
			}
		});
		this.aliasEditingProperty.addListener((ob, o, n) -> {
			if (n) {
				this.aliasCell.valueNodeProperty().set(this.aliasField);
				this.aliasField.setText(this.aliasEditProperty.get());
				this.aliasField.requestFocus();
			} else {
				this.aliasCell.valueNodeProperty().set(this.aliasText);
				this.aliasEditProperty.set(StringUtils.trimToEmpty(this.aliasField.getText()));
			}
		});
		this.displayNameEditingProperty.addListener((ob, o, n) -> {
			if (n) {
				this.displayNameCell.valueNodeProperty().set(this.displayNameField);
				this.displayNameField.setText(this.displayNameEditProperty.get());
				this.displayNameField.requestFocus();
			} else {
				this.displayNameCell.valueNodeProperty().set(this.displayNameText);
				this.displayNameEditProperty.set(StringUtils.trimToEmpty(this.displayNameField.getText()));
			}
		});
		this.contactProperty.addListener((ob, o, n) -> {
			if (n != null) {
				this.nameEditProperty.set(n.getNickName());
				this.aliasEditProperty.set(n.getAlias());
			}
		});
		this.chatroomProperty.addListener((ob, o, n) -> {
			if (n != null) {
				this.displayNameEditProperty.set(n.getDisplayName());
			}
		});
	}

	@Override
	public void initializeFinish() {
		this.nameCell.valueNodeProperty().set(this.nameText);
		this.nameCell.opsNodeProperty().set(this.nameIcon);

		this.announcementCell.valueNodeProperty().set(this.announcementText);
		this.announcementCell.opsNodeProperty().set(this.announcementIcon);

		this.aliasCell.valueNodeProperty().set(this.aliasText);
		this.aliasCell.opsNodeProperty().set(this.aliasIcon);

		this.displayNameCell.valueNodeProperty().set(this.displayNameText);
		this.displayNameCell.opsNodeProperty().set(this.displayNameIcon);
	}

	private final BooleanProperty nameEditingProperty = new SimpleBooleanProperty(false);
	private final BooleanProperty aliasEditingProperty = new SimpleBooleanProperty(false);
	private final BooleanProperty displayNameEditingProperty = new SimpleBooleanProperty(false);

	// ================================================================================
	// 编辑
	// ================================================================================
	private final StringProperty nameEditProperty = new SimpleStringProperty();

	public StringProperty nameEditProperty() {
		return nameEditProperty;
	}

	private final StringProperty announcementEditProperty = new SimpleStringProperty();

	public StringProperty announcementEditProperty() {
		return announcementEditProperty;
	}

	private final StringProperty aliasEditProperty = new SimpleStringProperty();

	public StringProperty aliasEditProperty() {
		return aliasEditProperty;
	}

	private final StringProperty displayNameEditProperty = new SimpleStringProperty();

	public StringProperty displayNameEditProperty() {
		return displayNameEditProperty;
	}

	// ================================================================================
	// 外部数据
	// ================================================================================
	private final ObjectProperty<ChatRoomDTO> chatroomProperty = new SimpleObjectProperty<>();
	private final ObjectProperty<ContactDTO> contactProperty = new SimpleObjectProperty<>();

	public ObjectProperty<ChatRoomDTO> chatroomProperty() {
		return chatroomProperty;
	}

	public ObjectProperty<ContactDTO> contactProperty() {
		return contactProperty;
	}
}
